const path = require('path')
const { defineConfig } = require('@vue/cli-service')
const { name } = require('./package');
// 导入compression-webpack-plugin
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const GitRevisionPlugin = require("git-revision-webpack-plugin");
const gitRevision = new GitRevisionPlugin();
const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
const moment = require('moment')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin


let Timestamp = moment().format('YYYY_MM_DD-HH_mm_ss')
// 有需要可以自行添加 ${process.VUE_CLI_SERVICE.mode}_
let gitInfo =  `${gitRevision.branch()}_${gitRevision.version()}`;
process.env.VUE_APP_GITINFO = gitInfo;
// 下面配配置项需和ng目录对应，独立服务的时候自行调整
const publicPath = process.env.NODE_ENV === 'development' ? process.env.VUE_APP_PUBLICPATH : `/micro-county-${process.env.VUE_APP_NAME}/`
function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = defineConfig({
    publicPath,
    outputDir:'dist',   // 打包输出目录    修改后需修改.gitignore 中的dist、dist.zip
    transpileDependencies: true,
    devServer: {
        port:process.env.VUE_APP_PORT,
        headers: {
            'Access-Control-Allow-Origin': '*',
        },
        // open: false,
        // disableHostCheck: true,
        // overlay: {
        //     warnings: false,
        //     errors: true
        // },
        proxy:{
            '/mock/9': {
                target: 'http://localhost:8011',  //api地址
                // target: 'http://xxx',//开发环境地址
                // target: 'http://xx',//测试环境地址
                changeOrigin: true,
                pathRewrite: {
                    '^/': '',
                },
            },
            "/api":{
                target: 'http://localhost:4001',  //api地址
                changeOrigin: true,
                pathRewrite: {
                    '^/': '',
                },
            }
        }
    },
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    '@ant-prefix': process.env.VUE_APP_NAME
                },
                javascriptEnabled: true
            }
        }
    },
    configureWebpack: {
        // 'eval-cheap-module-source-map' : "cheap-module-source-map",
        devtool: process.env.VUE_APP_DEVTOOL || "eval-cheap-module-source-map",
        output: {
            filename: `[name]-${gitInfo}-${Timestamp}.js`,
            chunkFilename: `[name]-${gitInfo}-${Timestamp}.js`,
            library: `${name}-[name]`,
            libraryTarget: 'umd',
            chunkLoadingGlobal: `webpackJsonp_${name}`,
        },
        plugins: [
            // new HotModuleReplacementPlugin(),
            //开启gzip打包
            new CompressionWebpackPlugin({
                algorithm: 'gzip',
                test: new RegExp('\\.(js|css)$'),
                threshold: 10240,
                minRatio: 0.8,
            }),
            // 打包前清除dist目录
            new CleanWebpackPlugin(),
            // 打包分析
            // new BundleAnalyzerPlugin()
        ],
        optimization: {
            minimizer: [
                new UglifyJsPlugin({
                    uglifyOptions: {
                        warnings: false,
                        compress: {
                            // drop_console: true,//console
                            drop_debugger: false,
                            // pure_funcs: ['console.log']//移除console
                        }
                    },
                }),
            ]
        },
    },
    chainWebpack(config) {
        config.module.rules.delete("svg"); // 重点：删除默认配置中处理svg,
        config.module
            .rule('svg-sprite-loader')
            .test(/\.svg$/)
            .include
            .add(resolve('src/assets/svg')) // 处理svg目录
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
    }
})
